package com.ljw.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import java.io.IOException;

/**
 * 将HDFS里面的数据导入到hbase
 * Created by admin on 2017/11/5.
 */
public class HDFSToHBase {
    public  static  class MyMapper extends Mapper<LongWritable,Text,ImmutableBytesWritable,Put>{
        ImmutableBytesWritable rowKey=new ImmutableBytesWritable();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//           hdfs的文件里的数据以“，”分割
           String[] lines= value.toString().split(",");
//           public Put(byte [] row)
           Put put =new Put(Bytes.toBytes(lines[0].trim()));
//           public Put add(byte [] family, byte [] qualifier, byte [] value)
           put.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("Q"),Bytes.toBytes(lines[1]));
           rowKey.set(Bytes.toBytes(lines[0].trim()));
           context.write(rowKey,put);
        }
    }

    public static void main(String[] args) throws Exception{
        Configuration config = HBaseConfiguration.create();
        Job job = Job.getInstance(config, "ExampleReadWrite");
//        设置job的处理类
        job.setJarByClass(HDFSToHBase.class);    // class that contains mapper
        job.setMapperClass(MyMapper.class);  //设置mapper class
        job.setMapOutputKeyClass(ImmutableBytesWritable.class);//设置mapper 的输出格式
        job.setOutputValueClass(Put.class);
        FileInputFormat.addInputPath(job,new Path(args[0]));
        TableMapReduceUtil.initTableReducerJob(
                args[1],      // output table输出的表的名称
                null,             // reducer class
                job);
        job.setNumReduceTasks(2);
        boolean b = job.waitForCompletion(true);
        if (!b) {
            throw new IOException("error with job!");
        }



    }

}
